﻿Ext.namespace('EHSubmit.Work');

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ext.namespace('EHSubmit.Work.Mark');

EHSubmit.Work.Mark.EastForm = {
    id: 'eastForm',
    title: '作业提交者的信息',
    xtype: 'form',
    region: 'east',
    split: true,
    width: 200,
    labelWidth: 50,
    labelAlign: 'top',
    collapsible: true,
    margins: '3 3 3 0',
    cmargins: '3 3 3 5',
    frame: true,

    //-----对于其的baseform获取的json数据的读取----------
    reader: new Ext.data.JsonReader(
     { root: 'data' },
     [
        { name: 'name', mapping: 'Name' },
        { name: 'sex', mapping: 'Sex' },
        { name: 'institute', mapping: 'Institute' },
        { name: 'department', mapping: 'Department' },
        { name: 'class', mapping: 'Class' }
     ]),

    defaultType: 'textfield',
    defaults: { width: 180, emptyText: '未知', readOnly: true },
    items: [{
        fieldLabel: '姓名',
        name: 'name'
    }, {
        fieldLabel: '性别',
        name: 'sex'
    }, {
        fieldLabel: '学院',
        name: 'institute'
    }, {
        fieldLabel: '专业',
        name: 'department'
    }, {
        fieldLabel: '班级',
        name: 'class'
    }]
}




EHSubmit.Work.Mark.WestForm = {
    id: 'westForm',
    title: '作业信息',
    xtype: 'form',
    region: 'west',
    margins: '3 0 3 3',
    collapsible: false,
    frame: true,
    width: 200,
    labelWidth: 150,
    labelAlign: 'top',
    defaultType: 'textfield',
    defaults: { width: 180, readOnly: true },

    //-----对于其的baseform获取的json数据的读取----------
    reader: new Ext.data.JsonReader(
        { root: 'data' },
        [
           { name: 'ID', mapping: 'ID' },
           //{ name: 'goalID', mapping: 'GoalID' },
           {name: 'goalName', mapping: 'GoalName' },
           { name: 'studentID', mapping: 'StudentID' },
           { name: 'submitTime', mapping: 'SubmitTime' },
           { name: 'updateTime', mapping: 'UpdateTime' },
           { name: 'attachPath', mapping: 'AttachPath' },
           { name: 'description', mapping: 'Description' },
           { name: 'scores', mapping: 'Scores' },
           //{ name: 'comment', mapping: 'Comment' },
    ]),

    items: [{
        xtype: 'hidden',
        name: 'ID'
    }, {
        name: 'goalName',
        fieldLabel: '作业名'
    }, {
        xtype: 'datefield',
        hideTrigger: true,
        format: 'Y年m月d日',
        name: 'submitTime',
        fieldLabel: '提交时间'
    }, {
        xtype: 'datefield',
        hideTrigger: true,

        format: 'Y年m月d日',
        name: 'updateTime',
        fieldLabel: '更新时间'
    }, {
        name: 'description',
        xtype: 'htmleditor',
        height: 140,
        fieldLabel: '说明',
        enableAlignments: false,
        enableColors: false,
        enableFont: false,
        enableFontSize: false,
        enableFormat: false,
        enableLinks: false,
        enableLists: false,
        enableSourceEdit:false
    }, {
        id: 'downloadButton',
        xtype: 'button',
        text: '附件下载',
        autoWidth: true,
        disabled: false,
        tooltip: '请点击此处',
        iconCls: 'download',
        handler: function() {
            var hwID = Ext.getCmp('westForm').getForm().findField('ID').getValue();
            window.open(EHSubmit.appRoot + 'Download.ashx?type=1&id=' + hwID);
        }
    }, {
        id: 'nothing',
        xtype: 'label',
        hideLabel: true,
        text: '无附件存在!',
        labelSeparator: '',
        style: 'color:red',
        hidden: true,
        hideMode: 'display'
    }]
}

EHSubmit.Work.Mark.CenterForm = {
    title: '作业评分',
    xtype: 'panel',
    region: 'center',
    margins: '3 -2 3 3',
    frame: true,
    items: [{
        id: 'centerForm',
        xtype: 'form',
        width: 200,
        labelWidth: 65,
        //labelAlign: 'left',
        buttonAlign: 'right',

        //-----居中设置----
        style: 'margin:0 auto',
        //bodyStyle: 'text-align:center',

        //---mapping的是record.data对象中的属性
        reader: new Ext.data.JsonReader(
            { root: 'data' },
            [
                { name: 'hwID', mapping: 'ID' },
                { name: 'percentileScore', mapping: 'scores' },
                { name: 'comment', mapping: 'comment' }
        ]),

        items: [{
            xtype: 'fieldset',
            title: '评分制',
            items: [{
                id: 'scoreSys',
                xtype: 'radiogroup',
                hideLabel: true,
                items: [
                    { itemId: 'percentile', boxLabel: '百分制', name: 'rb-auto', checked: true },
                    { itemId: 'five-tier', boxLabel: '五级制', name: 'rb-auto' }
                ],
                listeners: {
                    change: function(thisRdGroup, checkedRd) {
                        var rdID = checkedRd.getItemId();
                        var centerForm = Ext.getCmp('centerForm');
                        var five_tier = centerForm.getComponent('five-tier');
                        var percentile = centerForm.getComponent('percentile');
                        switch (rdID) {
                            case 'five-tier':
                                {
                                    five_tier.show();  //hidden值
                                    five_tier.getEl().up('.x-form-item').child('.x-form-item-label').setDisplayed('block');  //显示label   hideLabel值
                                    percentile.hide();
                                    percentile.getEl().up('.x-form-item').child('.x-form-item-label').setDisplayed('none');  //隐藏label
                                    break;
                                }
                            case 'percentile':
                                {
                                    five_tier.hide();
                                    five_tier.getEl().up('.x-form-item').child('.x-form-item-label').setDisplayed('none'); //隐藏label
                                    percentile.show();
                                    percentile.getEl().up('.x-form-item').child('.x-form-item-label').setDisplayed('block');  //显示label
                                    break;
                                }
                        }
                    }
                }
            }]
            }, {
                //-----作业ID---------------
                itemId: 'hwID',
                xtype: 'hidden',
                name: 'hwID'
            }, {
                //-----主要是方便五级制和百分制时传的分数-----
                itemId: 'score',
                xtype: 'hidden',
                name: 'score',
                value: '-1'
            }, {
                name: 'percentileScore',
                itemId: 'percentile',
                fieldLabel: '请填写分数',
                xtype: 'numberfield',
                allowBlank: false,
                blankText: '分数尚未填写!',
                value: '90',   //默认值
                width: 80,
                allowNegative: false,
                allowDecimals: false,
                decimalPrecision: 4,
                minValue: 0,
                maxValue: 100,
                maxText: '分数最大值应为100!',
                maskRe: /\d/
            }, {
                itemId: 'five-tier',
                xtype: 'combo',
                fieldLabel: '请选择:',
                width: 60,
                editable: false,
                value: '90',   //默认值
                mode: 'local',
                hidden: true,
                hideLabel: true,
                hidemode: 'display',

                /*tl     The top left corner (default)
                t      The center of the top edge
                tr     The top right corner
                l      The center of the left edge
                c      In the center of the element
                r      The center of the right edge
                bl     The bottom left corner
                b      The center of the bottom edge
                br     The bottom right corner*/
                //listAlign: 'br?',
                triggerAction: 'all',
                valueField: 'percentile',
                displayField: 'five-tier',

                //*************五分制具体对应的百分制的值要根据实际需要调整**************
                store: new Ext.data.SimpleStore({
                    fields: ['percentile', 'five-tier'],
                    data: [
                        ['90', '优'],     //可能有老师或者学生,但要考虑隐私
                        ['80', '良'],
                        ['70', '中'],
                        ['60', '差'],
                        ['50', '不及']
                    ]
                })
            }, {
                xtype: 'label',
                fieldLabel: '作业评论'
            }, {
                itemId: 'comment',
//                xtype: 'textarea',
                xtype:'htmleditor',
                name: 'comment',
                width: 198,
                height: 150,
                hideLabel: true,

                enableAlignments: false,
                enableColors: false,
                enableFont: false,
                enableFontSize: false,
                enableFormat: false,
                enableLinks: false,
                enableLists: false,
                enableSourceEdit: false
            }]
        }],
        buttons: [{
            id: 'submitButton',
            text: '提交',
            handler: function() {
                var centerForm = Ext.getCmp('centerForm');
                //----获取选定的radiobutton的id
                var rdID = Ext.getCmp('scoreSys').getValue().getItemId();
                var score = centerForm.getComponent('score');
                //var hwID = centerForm.getComponent('hwID');

                switch (rdID) {
                    case 'five-tier':
                        {
                            var five_tier = centerForm.getComponent('five-tier');
                            score.setValue(five_tier.getValue());
                            break;
                        }
                    case 'percentile':
                        {
                            var percentile = centerForm.getComponent('percentile');
                            score.setValue(percentile.getValue());
                            break;
                        }
                }

                if (centerForm.getForm().isValid()) {  //验证
                    centerForm.getForm().submit({  //提交
                        url: EHSubmit.ajaxRoot + 'HomeworkHandler.ashx?Op=Mark',
                        waitTitle: '等待',
                        waitMsg: '正在提交，请稍后...',
                        method: 'POST',
                        success: function(form, action) {
                            var successed = action.result.success;
                            if (successed) {
                                ///////////////  提交成功处理  ///////////////////
                                Ext.MessageBox.alert('提示', action.result.msg);
                            }
                            else {
                                Ext.MessageBox.alert('失败', '信息:' + action.result.msg);
                            }
                        },
                        failure: function(form, action) {
                            var errmsg = null;
                            switch (action.failureType) {
                                case Ext.form.Action.CLIENT_INVALID:
                                    errmsg = 'Form fields may not be submitted with invalid values';
                                    break;
                                case Ext.form.Action.CONNECT_FAILURE:
                                    errmsg = 'Ajax communication failed';
                                    break;
                                case Ext.form.Action.SERVER_INVALID:
                                    errmsg = action.result.msg;
                            }
                            Ext.MessageBox.alert('错误', errmsg);
                        }   // failure  
                    });
                }
            }
        }, {
            text: '重置',
            handler: function() {
                //----hwID无需重置----------
                var centerForm = Ext.getCmp('centerForm');
                Ext.getCmp('scoreSys').reset();
                centerForm.getComponent('score').reset();
                centerForm.getComponent('five-tier').reset();
                centerForm.getComponent('percentile').reset();
                centerForm.getComponent('comment').reset();
            }
        }]
}

//数据源
EHSubmit.Work.Mark.Store = new Ext.data.Store({
    baseParams: {
        Op: 'list_teacher',
        goalID: '',
        type: ''
    },      // 需要传递操作参数
    proxy: new Ext.data.HttpProxy({ url: EHSubmit.ajaxRoot + 'HomeworkHandler.ashx' }),  //后台具体处理

    reader: new Ext.data.JsonReader({
        totalProperty: 'count',    //总记录个数
        root: 'data'
    },
    [   // 数据模型
        {name: 'ID', mapping: 'ID' },
    //{ name: 'goalID', mapping: 'GoalID' },
        {name: 'goalName', mapping: 'GoalName' },
        { name: 'studentID', mapping: 'StudentID' },
        { name: 'submitTime', mapping: 'SubmitTime' },
        { name: 'updateTime', mapping: 'UpdateTime' },
        { name: 'attachPath', mapping: 'AttachPath' },
        { name: 'description', mapping: 'Description' },
        { name: 'scores', mapping: 'Scores' },
        { name: 'comment', mapping: 'Comment' }
    ]),
    listeners: {
        //----获取载入的作业记录(单个)------------
        load: function(thisStore, records, opt) {
            var hWork = records[0];

            //-----对westForm的刷新-------------
            if (hWork.data.description.length > 0) {
                var tmp = hWork.data.description;
                hWork.data.description = tmp.trim();
            }
            var westForm = Ext.getCmp('westForm');
            westForm.getForm().loadRecord(hWork);
            if (hWork.data.attachPath.length > 0) {
                westForm.getComponent('downloadButton').show();
                westForm.getComponent('nothing').hide();
            }
            else {
                westForm.getComponent('downloadButton').hide();
                westForm.getComponent('nothing').show();
            }

            //---对eastForm的刷新-------------
            var stuID = hWork.data.studentID;
            var eastForm = Ext.getCmp('eastForm');
            eastForm.getForm().load({
                url: EHSubmit.ajaxRoot + 'UserHandler.ashx',
                params: {
                    Op: 'GetUser',
                    stuID: stuID
                },
                method: 'GET'
                //waitMsg: 'Loading' 
            });

            //---对centerForm的刷新---------------
            var centerForm = Ext.getCmp('centerForm');
            //*****作业尚未评分---------
            if (hWork.data.scores == -1) {
                //Ext.getCmp('centerForm').getForm().reset();
                centerForm.getComponent('hwID').setValue(hWork.data.ID);
                centerForm.getComponent('score').reset();
                centerForm.getComponent('five-tier').reset();
                centerForm.getComponent('percentile').reset();
                centerForm.getComponent('comment').reset();

                Ext.getCmp('submitButton').setText('提交');

            }
            //*****作业已评分---------
            else {
                centerForm.getForm().loadRecord(hWork);
                centerForm.getComponent('hwID').setValue(hWork.data.ID);
                Ext.getCmp('scoreSys').reset();
                centerForm.getComponent('five-tier').reset();
                centerForm.getComponent('percentile').reset();

                //----percentile原先有默认值，可能会覆盖，需另外将其赋值--------------
                centerForm.getComponent('percentile').setValue(hWork.data.scores);

                Ext.getCmp('submitButton').setText('更新(已评)');

            }
        },
        /// 加载异常处理
        exception: function(proxy, type, action, options, response) {
            Ext.MessageBox.alert('数据加载错误!', '请与管理员联系');
        }
    }
});

//作业批改窗体
EHSubmit.Work.Mark.WinConfig = {
    id: 'markWorkWin',
    title: '作业批改',
    width: 650,
    height: 450,
    layout: 'border',
    closable: true,
    plain: true,
    //border: false,
    //autoHeight: true,
    modal: true,

    items: [EHSubmit.Work.Mark.WestForm, EHSubmit.Work.Mark.CenterForm, EHSubmit.Work.Mark.EastForm],
    bbar: {
        xtype: 'paging',
        pageSize: 1,                 //只是起计算页数的作用，不影响表面,内部影响start 和 limit
        store: EHSubmit.Work.Mark.Store,
        displayInfo: true,
        displayMsg: '显示第 {0} 条记录，一共 {2} 条',
        emptyMsg: '没有记录'
    },

    listeners: {
        activate: function(thisWin) {
            //要批改的作业信息不应从grid获取，因为批改的也可能不是当前页的作业
            var WinStore = EHSubmit.Work.Mark.Store;
            WinStore.setBaseParam('goalID', EHSubmit.Work.goalID);
            if (EHSubmit.Work.isMarking) {
                WinStore.setBaseParam('type', 0);
            }
            else {
                WinStore.setBaseParam('type', 1);
            }
            WinStore.load({ params: { start: EHSubmit.Work.hwIndex, limit: 1} });
        },
        hide: function() {
            if (EHSubmit.Work.isMarking) {
                Ext.getCmp('ListMarkingHomeworkGrid').store.reload();
            }
            else {
                Ext.getCmp('ListMarkedHomeworkGrid').store.reload();
            }
        }
    }
}

